<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>${table.tableNameExt1}Detail</title>
    <link rel="stylesheet" href="../layui/css/layui.css"  media="all">
</head>
<body>
<br>
<br>
<form class="layui-form" id="add-form" lay-filter="${table.tableNameExt1}Form" action="">

    <#list table.columns as column>
    <#if column.isPk =="1">
    <div class="layui-hide" >
        <label class="layui-form-label">ID</label>
        <div class="layui-input-block">
            <input type="text" name="${column.columnNameExt}" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <#else>
    <#if column.columnType=="Date">
    <div class="layui-form-item">
        <label class="layui-form-label">${column.columnComment}</label>
        <div class="layui-input-inline">
            <input type="text" name="${column.columnNameExt}" id="${column.columnNameExt}" lay-verify="datetime" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
        </div>
    </div>
    <#else>
    <div class="layui-form-item">
        <label class="layui-form-label">${column.columnComment}</label>
        <div class="layui-input-block">
            <input type="text" name=${column.columnNameExt} required  lay-verify="required" placeholder="请输入${column.columnComment}" autocomplete="off" class="layui-input">
        </div>
    </div>
</#if>
</#if>
</#list>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</div>
</form>
<script src="../layui/layui.js" charset="utf-8"></script>

<script>
    //Demo
    layui.use(['form','laydate','layer'], function(){
        var $ = layui.jquery;
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;

    <#list table.columns as column>
        <#if column.columnType=="Date">
        laydate.render({
            elem: '<#noparse>#</#noparse>${column.columnNameExt}'//指定元素
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm:ss'
        });
    </#if>
        </#list>

        var ${table.primaryKeyExt} = getQueryString("${table.primaryKeyExt}");
        if (null!=${table.primaryKeyExt}){
            $.ajax({
                type: 'get',
                url: '/${projectName}/${ClassName?uncap_first}/getById',
                dataType:'json',
                data: {"${table.primaryKeyExt}":${table.primaryKeyExt}},
                contentType: "application/json; charset=utf-8",
                success:function(res){//res为相应体,function为回调函数
                    //给表单赋值
                    form.val("${table.tableNameExt1}Form", {
                        <#list table.columns as column>
                    <#if column.isPk=="1">
                    "${column.columnNameExt}": res.${column.columnNameExt}
                <#else>
                        ,"${column.columnNameExt}": res.${column.columnNameExt}
                </#if>
                    </#list>
                });
                },
                error:function(res){
                    layer.alert(res.responseJSON.message,{icon:5});
                }
            });
        }


        //监听提交
        form.on('submit(formDemo)', function(data){
            //layer.msg(JSON.stringify(data.field));
            var ${table.primaryKeyExt} = data.field.${table.primaryKeyExt};

            if (null!=${table.primaryKeyExt} && ""!==${table.primaryKeyExt}){
                edit(data);
            }else {
                add(data);
            }
            return false;
        });
    });

    function add(data) {
        var $ = layui.jquery;
        $.ajax({
            type: 'post',
            url: '/${projectName}/${ClassName?uncap_first}/create',
            dataType:'json',
            data:JSON.stringify(data.field),
            contentType: "application/json; charset=utf-8",
            success:function(res){//res为相应体,function为回调函数
                layer.msg("新增成功!", {
                    icon: 6,
                    time: 500
                }, function () {
                    parent.window.location.reload();
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                });
            },
            error:function(res){
                layer.alert(res.responseJSON.message,{icon:5});
            }
        });
    }

    function edit(data) {
        var $ = layui.jquery;
        $.ajax({
            type: 'put',
            url: '/${projectName}/${ClassName?uncap_first}/update',
            dataType:'json',
            data:JSON.stringify(data.field),
            contentType: "application/json; charset=utf-8",
            success:function(res){//res为相应体,function为回调函数
                layer.msg("修改成功!", {
                    icon: 6,
                    time: 500
                }, function () {
                    parent.window.location.reload();
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                });
            },
            error:function(res){
                layer.alert(res.responseJSON.message,{icon:5});
            }
        });
    }

    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
</script>

</body>
</html>